package com.beehoo.dcportal.admin.dao.impl;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.beehoo.dcportal.admin.dao.RelatedLinkDao;
import com.beehoo.dcportal.common.base.BaseDao;
import com.beehoo.dcportal.common.base.BaseException;
import com.beehoo.dcportal.model.RelatedLinkBean;

public class RelatedLinkDaoImpl extends BaseDao implements RelatedLinkDao {

	private static final Logger logger = Logger
			.getLogger(RelatedLinkDaoImpl.class);
	
	@Override
	public String createRelatedLink(RelatedLinkBean relatedLinkBean)
			throws BaseException {
		String id = null;
		try {
			id = (String) sqlMapClient.insert("admin-link.addRelatedLink", relatedLinkBean);
			logger.info("友情链接管理-添加友情链接|#" +
					"[中心编号：" + relatedLinkBean.getStreetId() + "]," +
					"[友情链接名称：" + relatedLinkBean.getLinkName() + 
					"[友情链接路径：" + relatedLinkBean.getLinkPath() + 
					"[友情链接类型：" + relatedLinkBean.getLinkType() + 
					"],成功!");
		} catch (SQLException e) {
			logger.error("友情链接管理-添加友情链接|#" +
					"[中心编号：" + relatedLinkBean.getStreetId() + "]," +
					"[友情链接名称：" + relatedLinkBean.getLinkName() + 
					"[友情链接路径：" + relatedLinkBean.getLinkPath() + 
					"[友情链接类型：" + relatedLinkBean.getLinkType() + 
					"],失败!异常：" + e.getMessage());
			throw new BaseException(e,
					"custom.messages.error.link.create.error");
		}
		return id;
	}

	@Override
	public int deleteRelatedLink(String linkId) throws BaseException {
		int ret = 0;
		try {
			ret = sqlMapClient.delete("admin-link.delRelatedLink", linkId);
			logger.info("友情链接管理-删除友情链接|#" +
					"[友情链接编号：" + linkId + "]," +
					"],成功!");
		} catch (SQLException e) {
			logger.error("友情链接管理-删除友情链接|#" +
					"[友情链接编号：" + linkId + "]," +
					"],失败!异常：" + e.getMessage());
			throw new BaseException(e,
					"custom.messages.error.link.del.error");
		}
		return ret;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<RelatedLinkBean> queryRelatedLink(Map<String, Object> paramMap)
			throws BaseException {
		List<RelatedLinkBean> beanLs = null;
		try {
			beanLs = sqlMapClient.queryForList("admin-link.getRelatedLinkList", paramMap);
			logger.info("友情链接管理-查询友情链接|#" +
					"[中心编号：" + paramMap.get("streetId") + "]," +
					"[友情链接类型：" + paramMap.get("linkType") + "]," +
					"],成功!");
		} catch (SQLException e) {
			logger.error("友情链接管理-查询友情链接|#" +
					"[中心编号：" + paramMap.get("streetId") + "]," +
					"[友情链接类型：" + paramMap.get("linkType") + "]," +
					"],失败!异常：" + e.getMessage());
			throw new BaseException(e,
					"custom.messages.error.link.query.error");
		}
		return beanLs;
	}

}
